PayService = require './PayService'
log = require '../log'

# 处理支付宝支付通知
exports.gAlipayCallback = (ctx, data)->
    paymentId = data["out_trade_no"]
    responseCode = data["trade_status"]
    providerFlowNo = data["trade_no"]
    amount = data['total_fee'] * 100 # 单位元到分
    buyer = data['buyer_email']
    state = if responseCode == 'TRADE_SUCCESS' || responseCode == 'TRADE_FINISHED'
        PayService.states.Success
    else null

    if state
        yield from PayService.gUpdatePaymentState(ctx, paymentId, state, providerFlowNo, amount, buyer)
    else
        log.pay.info 'Not process callback state, Alipay', {responseCode, paymentId, providerFlowNo}